﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.Collections.ObjectModel;
using System.Windows.Navigation;

namespace AQMProject
{

    /// <summary>
    /// Interaktionslogik für checkBerechnung.xaml
    /// </summary>
    public partial class checkBerechnung : Window
    {
        public class DataObject
        {
            public String A { get; set; } //Datum
            public String B { get; set; } //Uhrzeit
            public String C { get; set; } //Empfehlung von
            public String D { get; set; } //Empfehlung
            public String E { get; set; } //Kurs
            public String F { get; set; } //Aktienbestand
            public String G { get; set; } //Trade
            public String H { get; set; } //Kosten
            public String I { get; set; } //Kontostand
            public String J { get; set; } //Aktienwert
            public String K { get; set; } //GuV
        }
          
        private DateTime startDate;
        private DateTime endDate;

        private String symbol;
        private String bank;

        private Decimal maxInvest;
        private String strategie;

        private bool faktoren;
        private bool buysell;
        private bool sellall;

        public checkBerechnung()
        {
            InitializeComponent();
        }

        public void InitForm()
        {
            this.faktoren = false;
            this.buysell = false;

            this.maxInvest = 1000000;
            this.maxInvestTextBox.Text = this.maxInvest.ToString();

            //Startdatum füllen.
            this.startdate.SelectedDate = this.startDate;

            //Enddatum füllen.
            this.enddate.SelectedDate = this.endDate;

            //Aktie füllen.
            //ComboBoxAktie füllen.
            AktieManager aktiemanager = new AktieManager();
            List<aktie> aktielist = aktiemanager.GetAktienList();

            int i = 0;

            foreach (aktie aktieentry in aktielist)
            {
                this.ComboBoxAktien.Items.Add(aktieentry.aktienname);

                if (aktieentry.aktienkuerzel == this.symbol)
                {
                   this.ComboBoxAktien.SelectedIndex = i;
                }
                i++;
            }

            //Bank füllen.
            //ComboBoxBank füllen.
            BankManager bankmanager = new BankManager();
            List<bank> banklist = bankmanager.getBankList();

            i = 0;

            foreach (bank bankentry in banklist)
            {
                this.ComboBoxBank.Items.Add(bankentry.bankname);

                if (bankentry.bankname == this.bank)
                {
                    this.ComboBoxBank.SelectedIndex = i;
                }
                i++;
            }

            //Strategie füllen.
            //ComboBoxStrategie füllen.
            this.ComboBoxStrategie.Items.Add("Normal");
            this.ComboBoxStrategie.Items.Add("Aggresiv");
            this.ComboBoxStrategie.Items.Add("Trade = 1000");
            //this.ComboBoxStrategie.Items.Add("Passiv");

            this.ComboBoxStrategie.SelectedIndex = 0;

        }

        private void ShowRssTable()
        {
            Uri url;
            RssManager rssmanager = new RssManager();
            BankManager bankmanager = new BankManager();

            List<rssuebersicht> rssuebersicht_liste = rssmanager.GetFiltertItems(this.startDate, this.endDate, this.symbol, bankmanager.getBankID(this.bank), this.buysell);
            var list = new ObservableCollection<DataObject>();

            //Init
            int aktienbestand = 0;
            Decimal kontostand = this.maxInvest;
             
            //Pro Trade
            int trade = 0;
            int standardtrade = 0;
            int maxtrade = 0;
            Decimal kostenTrade = 0;
            Decimal aktienwert = 0;
            Decimal guv = 0;
            Decimal tageskurs = 0;

            String action = "";
            Decimal faktor = 0;
            
            foreach (rssuebersicht rssuebersicht in rssuebersicht_liste)
            {
                
                //Tageskurs ermitteln
                url = YahooUrlGenerator.createURL(this.symbol, rssuebersicht.DatumUhrzeit, rssuebersicht.DatumUhrzeit);
                List<ShareData> kurse = YahooCsvLoader.readYahhoCsvFromWeb(url);
                List<KeyValuePair<DateTime, float>> valueList = new List<KeyValuePair<DateTime, float>>();

                foreach (ShareData kurs in kurse)
                {
                    tageskurs = Convert.ToDecimal(kurs.Close);
                }

                standardtrade = Convert.ToInt32(this.maxInvest/tageskurs/10);

                action = rssmanager.getAction(rssuebersicht.empfehlung_idempfehlung);
                
                //Trade berechnen
                switch (this.strategie)
                {
                    case "Normal":
                        trade = standardtrade;
                        break;
                    case "Aggresiv":
                        switch (action)
                        {
                            case "Buy":
                                trade = standardtrade*4;
                                break;
                            case "Sell":
                                trade = aktienbestand;//Convert.ToInt32(this.maxInvest / tageskurs);
                                break;
                            default:
                                trade = 0;
                                break;
                        }
                        break;
                    case "Trade = 1000":
                        trade = 1000;
                        break;
                    default:
                        trade = 1000;
                        break;
                }

                
                if (this.faktoren)
                {
                    faktor = rssmanager.getFaktor(rssuebersicht.empfehlung_idempfehlung);
                    trade = Convert.ToInt32(trade * faktor);
                }

                switch (action)
                {
                    case "Buy":
                        //trade = trade;
                        maxtrade = Convert.ToInt32(kontostand / tageskurs);
                        if (maxtrade < trade)
                        {
                            trade = maxtrade;
                        }
                        break;
                    case "Sell":
                        if (trade > aktienbestand)
                        {
                            trade = aktienbestand;
                        }

                        if (this.sellall)
                        {
                            trade = aktienbestand;
                        }

                        trade = trade * (-1);
                        break;
                    default:
                        trade = 0;
                        break;
                }

                //Berechnungen
                kostenTrade = (tageskurs * trade);
                aktienbestand += trade;
                kontostand -= kostenTrade;
                aktienwert = aktienbestand * tageskurs;
                guv = aktienwert + kontostand - this.maxInvest;
                
                list.Add(new DataObject()
                {
                    //Datum
                    A = rssuebersicht.DatumUhrzeit.ToShortDateString(),
                    //Uhrzeit
                    B = rssuebersicht.DatumUhrzeit.ToShortTimeString(),
                    //Empfehlung von
                    C = bankmanager.getBankName(rssuebersicht.bank_idbank),
                    //Empfehlung
                    D = rssmanager.getEmpfehlung(rssuebersicht.empfehlung_idempfehlung),
                    //Kurs
                    E = tageskurs.ToString(),
                    //Aktienbestand
                    F = aktienbestand.ToString(),
                    //Trade
                    G = trade.ToString(),
                    //Kosten
                    H = kostenTrade.ToString(),
                    //Kontostand
                    I = kontostand.ToString(),
                    //Aktienwert
                    J = aktienwert.ToString(),
                    //GuV
                    K = guv.ToString()
                });
            }

            //Tageskurs ermitteln
            url = YahooUrlGenerator.createURL(this.symbol, Convert.ToDateTime(this.endDate), Convert.ToDateTime(this.endDate));
            List<ShareData> kurse2 = YahooCsvLoader.readYahhoCsvFromWeb(url);
            List<KeyValuePair<DateTime, float>> valueList2 = new List<KeyValuePair<DateTime, float>>();

            foreach (ShareData kurs in kurse2)
            {
                tageskurs = Convert.ToDecimal(kurs.Close);
            }

            guv = aktienwert + kontostand - this.maxInvest;
            guv = tageskurs * aktienbestand + kontostand - this.maxInvest;

            this.gewinnverlust.Text = guv.ToString();
            this.neuerkontostand.Text = (tageskurs * aktienbestand + kontostand).ToString();
            this.dataGrid1.ItemsSource = list;
        }

        private void Berechnen_Click(object sender, RoutedEventArgs e)
        {
            ShowRssTable();
        }

        //Formular aktualiesieren
        
        private void Refresh_Click(object sender, RoutedEventArgs e)
        {
            ShowRssTable();
        }

        //Werte Änderungen abfangen

        private void ComboBoxAktien_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            AktieManager aktiemanager = new AktieManager();
            this.symbol = aktiemanager.GetAktienkuerzel(this.ComboBoxAktien.SelectedItem.ToString());
        }

        private void ComboBoxBank_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.bank = this.ComboBoxBank.SelectedItem.ToString();
        }

        private void ComboBoxStrategie_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.strategie = this.ComboBoxStrategie.SelectedItem.ToString();
        }

        private void startdate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            this.startDate = Convert.ToDateTime(sender.ToString());
        }

        private void enddate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            this.endDate = Convert.ToDateTime(sender.ToString());
        }

        private void setMaxInvest(object sender, DependencyPropertyChangedEventArgs e)
        {
            this.maxInvest = Convert.ToDecimal(this.maxInvestTextBox);
        }

        //Public Funktionen um Daten auszutauschen

        public void setParameter(DateTime p_startDate, DateTime p_endDate, String p_symbol, String p_bank)
        {
            this.startDate = p_startDate;
            this.endDate = p_endDate;
            this.symbol = p_symbol;
            this.bank = p_bank;
        }

        private void setFaktor(object sender, RoutedEventArgs e)
        {
            this.faktoren = this.Checkbox_faktor.IsChecked.Value;
        }

        private void setBuySell(object sender, RoutedEventArgs e)
        {
            this.buysell = this.Checkbox_buysell.IsChecked.Value;
        }

        private void setSellAll(object sender, RoutedEventArgs e)
        {
            this.sellall = this.Checkbox_sellall.IsChecked.Value;
        }

    }
}

